package com.aube.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aube.R;
import com.aube.model.OptionResultModel;
import com.aube.model.TimeLinePiece;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.model.ChannelDetail;
import com.huyn.baseframework.utils.SysUtil;
import com.makeramen.roundedimageview.RoundedImageView;

/**
 * Created by huyaonan on 16/3/18.
 */
public class CardPicNew extends BaseCardNew {

    private int margin = 0;
    private int marginBottom = 0;

    private RoundedImageView mImg;

    public CardPicNew(Context context) {
        this(context, null);
    }

    public CardPicNew(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardPicNew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        int screenWidth = SysUtil.getScreenWidth(context);
        int width = screenWidth*3/5;
        int height = width*180/800;

        View root = LayoutInflater.from(getContext()).inflate(R.layout.layout_card_pic, null);

        addView(root, new ViewGroup.LayoutParams(width, height));

        mImg = (RoundedImageView) findViewById(R.id.card_pic_img);

        findViewById(R.id.card_pic_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });

        margin = getResources().getDimensionPixelOffset(R.dimen.card_margin_space);

//        marginBottom = getResources().getDimensionPixelOffset(R.dimen.card_margin_bottom);
    }


    @Override
    public void init(TimeLinePiece data, ChannelDetail detail) {
        super.init(data, detail);
        String url = data.detail.pic.picurl;
        ImageLoader.getInstance(getContext()).fillImage(mImg, url);
    }

    @Override
    public String getType() {
        return TYPE_PIC;
    }

    @Override
    public void syncData(OptionResultModel response) {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = mImg.getMeasuredHeight() + marginBottom;
        int width = mImg.getMeasuredWidth();

        measureChild(getChildAt(0), View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));

        setMeasuredDimension(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
    }

}
